package cn.bugDance.domain.rebate.service;

import cn.bugDance.domain.activity.model.entity.ActivityAccountEntity;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.rebate.model.aggregate.UserBehaviorRebateOrderAggregate;
import cn.bugDance.domain.rebate.model.entity.BehaviorEntity;
import cn.bugDance.domain.rebate.model.entity.DallyBehaviorRebateEntity;
import cn.bugDance.domain.rebate.model.entity.UserBehaviorRebateOrderEntity;
import cn.bugDance.domain.rebate.model.valobj.BehaviorVO;
import cn.bugDance.domain.rebate.repository.IRebateRepository;
import cn.bugDance.domain.rebate.service.rule.factory.RuleChainFactory;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
public abstract class AbstractRebateService implements IRebateService{

    @Autowired
    @Qualifier("rebateRuleChainFactory")
    private RuleChainFactory ruleChainFactory;

    @Resource
    private IRebateRepository rebateRepository;
    @Resource
    private IActivityRepository activityRepository;


    @Override
    public void createUserBehaviorRebateOrder(BehaviorEntity behaviorEntity) {
        String userId = behaviorEntity.getUserId();
        String behaviorType = behaviorEntity.getBehaviorType();
        Long activityId = behaviorEntity.getActivityId();
        String signDay = behaviorEntity.getSignDay();
        String signMonth = behaviorEntity.getSignMonth();

        if(StringUtils.isBlank(behaviorType) || StringUtils.isBlank(signDay) || StringUtils.isBlank(userId) || null == activityId || StringUtils.isBlank(signMonth)) {
            log.error("【返利服务层-AbstractRebateService】: 参数校验错误");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        ActivityAccountEntity activityAccountEntity = activityRepository.queryActivityAccountByUserIdAndActivityId(userId, activityId);
        if(null == activityAccountEntity) {
            throw new AppException(ResponseCode.ACTIVITY_ACCOUNT_TOTAL_COUNT_ERROR.getCode(), ResponseCode.ACTIVITY_ACCOUNT_TOTAL_COUNT_ERROR.getInfo());
        }


        List<DallyBehaviorRebateEntity> behaviorRebateEntities = ruleChainFactory.openRuleChain(behaviorEntity);
        if(behaviorRebateEntities == null || behaviorRebateEntities.isEmpty()){
            return;
        }

        UserBehaviorRebateOrderAggregate userBehaviorRebateOrderAggregate = buildUserBehaviorRebateOrderAggregates(behaviorRebateEntities, behaviorEntity);
        doSaveOrders(userBehaviorRebateOrderAggregate, behaviorEntity);
    }


    protected abstract void doSaveOrders(UserBehaviorRebateOrderAggregate userBehaviorRebateOrderAggregate, BehaviorEntity behaviorEntity);

    protected abstract UserBehaviorRebateOrderAggregate buildUserBehaviorRebateOrderAggregates(List<DallyBehaviorRebateEntity> behaviorRebateEntities, BehaviorEntity behaviorEntity);
}
